package com.ruoyi.project.system.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.project.system.domain.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.service.IClResumeService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 简历上传Controller
 *
 * @author cenliey
 * @date 2024-05-04
 */
@RestController
@RequestMapping("/resume/resumeCreate")
public class ClResumeController extends BaseController {


    @Autowired
    private IClResumeService clResumeService;

//    @PostMapping"/test/form"
//    public void
    @PostMapping(value = "/test/file")
    public void insert(@RequestPart ResumeFormDto resumeFormDto) throws IOException {
        System.out.println("\n");
//        if(null == formData) {
//            System.out.println("o is null");
//            return;
//        }
//        formData.forEach((key, value) -> System.out.println(key + ": " + value));
        MultipartFile pdfFile = resumeFormDto.getPdfFile();

        System.out.println(resumeFormDto.getPersonInfo());
        System.out.println(pdfFile.getOriginalFilename());
//        System.out.println("\ninsert triggered\n");
//        if (null == pdfFile) {
//            System.out.println("file is null");
//            return;
//        }
//        System.out.println("\n"+resume.toString()+"\n");
//        ClResume resume = new ClResume();
//
//        String fileName = pdfFile.getOriginalFilename();
//        byte[] bytes = pdfFile.getBytes();
//        String filePath = "D:\\resumes\\" + fileName;
//        File serverFile = new File(filePath);
//        try {
//            serverFile.createNewFile();
//            try (FileOutputStream fileOutputStream = new FileOutputStream(serverFile)) {
//                fileOutputStream.write(bytes);
//            }
//            System.out.println("文件上传成功 " + fileName);
//        } catch (IOException e) {
//            System.out.println("test error function:ClResumeController.insert");
//            e.printStackTrace();
//        }
    }

    /**
     * 查询简历上传列表
     */
    @PreAuthorize("@ss.hasPermi('resume:resumeCreate:list')")
    @GetMapping("/list")
    public TableDataInfo list(ClResume clResume) {
        startPage();
        List<ClResume> list = clResumeService.selectClResumeList(clResume);
        return getDataTable(list);
    }

    /**
     * 导出简历上传列表
     */
    @PreAuthorize("@ss.hasPermi('resume:resumeCreate:export')")
    @Log(title = "简历上传", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ClResume clResume) {
        List<ClResume> list = clResumeService.selectClResumeList(clResume);
        ExcelUtil<ClResume> util = new ExcelUtil<ClResume>(ClResume.class);
        util.exportExcel(response, list, "简历上传数据");
    }

    /**
     * 获取简历上传详细信息
     */
    @PreAuthorize("@ss.hasPermi('resume:resumeCreate:query')")
    @GetMapping(value = "/{resumeId}")
    public AjaxResult getInfo(@PathVariable("resumeId") Long resumeId) {
        return success(clResumeService.selectClResumeByResumeId(resumeId));
    }

    /**
     * 新增简历上传
     */
    @PreAuthorize("@ss.hasPermi('resume:resumeCreate:add')")
    @Log(title = "简历上传", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClResume clResume) {
        return toAjax(clResumeService.insertClResume(clResume));
    }

    /**
     * 修改简历上传
     */
    @PreAuthorize("@ss.hasPermi('resume:resumeCreate:edit')")
    @Log(title = "简历上传", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ClResume clResume) {
        return toAjax(clResumeService.updateClResume(clResume));
    }

    /**
     * 删除简历上传
     */
    @PreAuthorize("@ss.hasPermi('resume:resumeCreate:remove')")
    @Log(title = "简历上传", businessType = BusinessType.DELETE)
    @DeleteMapping("/{resumeIds}")
    public AjaxResult remove(@PathVariable Long[] resumeIds) {
        return toAjax(clResumeService.deleteClResumeByResumeIds(resumeIds));
    }
}
